<template>
  <el-dialog
    :title="!dataForm.id ? 'create' : 'modify'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="Tag" prop="tag">
      <el-input v-model="dataForm.tag" placeholder="tag"></el-input>
    </el-form-item>
    <el-form-item label="Order" prop="ordindex">
      <el-input v-model="dataForm.ordindex" placeholder="order"></el-input>
    </el-form-item>
    <!-- <el-form-item label="" prop="leadsId">
      <el-input v-model="dataForm.leadsId" placeholder=""></el-input>
    </el-form-item> -->
    <el-form-item label="Type" prop="type">
      <el-input v-model="dataForm.type" placeholder="type"></el-input>
    </el-form-item>
    <el-form-item label="Remark" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="remark"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">Cancel</el-button>
      <el-button type="primary" @click="dataFormSubmit()">Yes</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          tagId: 0,
          tag: '',
          ordindex: '',
          leadsId: '',
          type: '',
          remark: ''
        },
        dataRule: {
          tag: [
            { required: true, message: 'Not null', trigger: 'blur' }
          ],
          ordindex: [
            { required: true, message: 'Not null', trigger: 'blur' }
          ],
          type: [
            { required: true, message: 'Not null', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: 'Not null', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.tagId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.tagId) {
            this.$http({
              url: this.$http.adornUrl(`/crm/crmtags4leads/info/${this.dataForm.tagId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.tag = data.crmtags4leads.tag
                this.dataForm.ordindex = data.crmtags4leads.ordindex
                this.dataForm.leadsId = data.crmtags4leads.leadsId
                this.dataForm.type = data.crmtags4leads.type
                this.dataForm.remark = data.crmtags4leads.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/crm/crmtags4leads/${!this.dataForm.tagId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'tagId': this.dataForm.tagId || undefined,
                'tag': this.dataForm.tag,
                'ordindex': this.dataForm.ordindex,
                'leadsId': this.dataForm.leadsId,
                'type': this.dataForm.type,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: 'Success',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
